package com.jason.web.tag.controller;

import com.cn.lotary.base.dao.BaseDao;
import com.cn.lotary.base.web.BaseController;
import com.cn.lotary.util.LoggerUtils;
import com.jason.web.tag.dao.BaseCategoryDao;
import com.jason.web.tag.entity.Category;
import com.jason.web.tag.entity.Category_;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.persistence.metamodel.SingularAttribute;
import org.primefaces.component.menubar.Menubar;
import org.primefaces.model.DefaultTreeNode;
import org.primefaces.model.TreeNode;

/**
 *
 * @author Jason
 */
@ManagedBean
@ViewScoped
public class BaseCategoryBean extends BaseController<Category> {

    private List<Category> categorys;
    @EJB
    private BaseCategoryDao dao;
    private TreeNode tree;
    private Menubar menubar;
    private TreeNode selectNode;

    @PostConstruct
    public void init() {
        menubar = BaseWebUtils.convertCategoryToMenubar(dao.getRootNode());
        tree = BaseWebUtils.convertCategorysToTreeNode(new DefaultTreeNode("Root",null), dao.getRootNode());
    }

    public void addSameNode() {
        Category c = ((Category) selectNode.getData()).getParentCategory();
        Category entity = super.getEntity();
        entity.setParentCategory(c);
        super.setEntity(entity);
LoggerUtils.debug("addSameNode[entity.getName()] "+entity.getName());        
        super.create();        
    }

    //add 同级t
    public void addSubNode() {
        Category entity = super.getEntity();
        Category c = (Category) getSelectNode().getData();
  //      tree.getChildren().add(new DefaultTreeNode(selectNode.getParent(), (TreeNode) c));
        if (c != null) {
            entity.setParentCategory(c);
        }
        super.setEntity(entity);
        LoggerUtils.debug("[entity.getName()] "+entity.getName());
        super.create();
    }

    public void editNode() {
        setEntity((Category) getSelectNode());
        super.update();
    }

    public void removeNode() {
        super.remove((Category) selectNode.getData()); //To change body of generated methods, choose Tools | Templates.
        selectNode.getChildren().clear();
        selectNode.getParent().getChildren().remove(selectNode);
        selectNode.setParent(null);
    }

    public Menubar getMenubar() {
        return menubar;
    }

    public void setMenubar(Menubar menubar) {
        this.menubar = menubar;
    }

    public List<Category> getAll() {
        return dao.findAll();
    }

    public TreeNode getTree() {
        return tree;
    }

    public List<Category> getCategorys() {
        categorys = dao.getRootNode();
        return categorys;
    }

    public void setCategorys(List<Category> categorys) {
        this.categorys = categorys;
    }

    @Override
    protected BaseDao<Category> dao() {
        return dao;
    }

    @Override
    protected SingularAttribute selectItemLabel() {
        return Category_.name;
    }

    public TreeNode getSelectNode() {
        if (selectNode == null) {
            selectNode = new DefaultTreeNode();
        }
        return selectNode;
    }

    public void setSelectNode(TreeNode selectNode) {
        this.selectNode = selectNode;
    }
}
